package com.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.ContractMapper;
import com.dao.TypeMapper;
import com.dao.UserMapper;
import com.pojo.Contract;
import com.pojo.ContractExample;
import com.pojo.Type;
import com.pojo.User;
import com.service.ContractService;

@Service
public class ContractServiceImpl implements ContractService {

	@Autowired
	private ContractMapper contractMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private TypeMapper typeMapper;

	@Override
	public long countByExample(ContractExample example) {
		// TODO Auto-generated method stub
		return contractMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(ContractExample example) {
		// TODO Auto-generated method stub
		return contractMapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return contractMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Contract record) {
		// TODO Auto-generated method stub
		return contractMapper.insert(record);
	}

	@Override
	public int insertSelective(Contract record) {
		// TODO Auto-generated method stub
		return contractMapper.insertSelective(record);
	}

	@Override
	public List<Contract> selectByExample(ContractExample example) {

		List<Contract> contracts = contractMapper.selectByExample(example);

		User user = null;
		Type type = null;
		for (Contract contract : contracts) {
			user = userMapper.selectByPrimaryKey(contract.getUserRoleId());
			contract.setUser(user);
			contract.setUserName(user.getName());

			type = typeMapper.selectByPrimaryKey(contract.getTypeId());
			contract.setType(type);
			contract.setTypeName(type.getType());
		}

		// TODO Auto-generated method stub
		return contracts;
	}

	@Override
	public Contract selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		Contract contract = contractMapper.selectByPrimaryKey(id);

		User user = userMapper.selectByPrimaryKey(contract.getUserRoleId());
		contract.setUser(user);
		contract.setUserName(user.getName());

		Type type = typeMapper.selectByPrimaryKey(contract.getTypeId());
		contract.setType(type);
		contract.setTypeName(type.getType());

		return contract;
	}

	@Override
	public int updateByExampleSelective(Contract record, ContractExample example) {
		// TODO Auto-generated method stub
		return contractMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(Contract record, ContractExample example) {
		// TODO Auto-generated method stub
		return contractMapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(Contract record) {
		// TODO Auto-generated method stub
		return contractMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Contract record) {
		// TODO Auto-generated method stub
		return contractMapper.updateByPrimaryKey(record);
	}

}
